<template>
  <dialog-box
    :isDialog="false"
    ref="myDialog"
    :showFooter="true"
    width="500px"
    @close="close"
    @sureHandle="sureHandle"
    :isFooterSlot="true"
    title="分享"
  >
    <template v-slot:dialogMain>
      <div>
        <el-form
          ref="ruleFormRef"
          :model="ruleForm"
          :rules="rules"
          label-width="auto"
          class="demo-ruleForm"
          size="default"
          status-icon
        >
          <el-form-item label="所选团队" prop="source">
            <el-select v-model="ruleForm.source">
              <el-option
                v-for="(it, i) in state.teamTypes"
                :key="i"
                :label="it.key"
                :value="it.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="权限管理" prop="permission">
            <el-radio-group v-model="ruleForm.permission">
              <el-radio value="1">编辑</el-radio>
              <el-radio value="0">查看使用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
    </template>
  </dialog-box>
</template>

<script setup lang="ts">
import dialogBox from "@/components/dialog-box/index.vue";
import { onMounted, computed, reactive, toRefs, ref } from "vue";
import type { ElMessage, FormInstance, FormRules } from "element-plus";
import apiAxios from "@/api/SelfAnalysis/analysis-report-manage";
import { PAth } from "@/api/SelfAnalysis/query";
const ruleForm = ref(PAth());
const myDialog = ref(null);

const ruleFormRef = ref<FormInstance>();


let state = reactive({
  teamTypes: [
    { key: "我创建的", value: '0' },
    { key: "我团队创建的", value: '1' },
    { key: "分享给我的", value: '2' },
  ]
});
// 定义完成后，用defineExpose()暴露给父组件
const open = (id: string) => {
  ruleForm.value.id = id
  myDialog.value.handleOpen();
};
const close = () => {
  resetForm();
  myDialog.value.handleClose();
};
const sureHandle = () => {
  submitForm();
};
defineExpose({
  open,
  close,
});
const emit = defineEmits(['successHand'])

const submitForm = async () => {
  if (!ruleFormRef) return;
  await ruleFormRef.value?.validate(async (valid, fields) => {
    if (valid) {
      let res = await apiAxios.dataEdit(ruleForm.value);
      if (res) {
        ElMessage({
          type: "success",
          message: "操作成功!",
        });
        emit('successHand')
        close()
      }
    }
  });
};

const resetForm = () => {
  if (!ruleFormRef) return;
  ruleFormRef.value?.resetFields();
};
const rules = reactive<FormRules<RuleForm>>({
  source: [
    { required: true, message: "请选择团队", trigger: "blur" },
  ],
  permission: [
    { required: true, message: "请选择权限", trigger: "blur" },
  ]
});
</script>

<style scoped>
h2 {
  margin-bottom: 20px;
}
.mb-20 {
  margin-bottom: 20px;
}
.mr-10 {
  margin-right: 10px;
}
.ml-10 {
  margin-left: 10px;
}
.steps-box {
  width: 300px;
  margin: 0 auto 30px;
}
.title-box {
  display: flex;
  align-items: center;
}
.seach-box {
  display: flex;
  padding: 0 10px;
  box-sizing: border-box;
}
.footer-btns {
  width: 100%;
  display: flex;
  justify-content: center;
}
</style>
